begin tran
	select * into #a from igsnet.dbo.ms_style where size_group = '1218' 

	
	select style_no, min(size) min_size, max(size) max_size, '' size_group 
	into #style_size_is_str
	from pos_sku  a
	where exists(select *  from #a aa where a.style_no = aa.style_no ) and 
		isnumeric(a.size) = 0
	group by style_no

	select size_grp, min(size) min_size, max(size) max_size 
	into #size_str
	from ms_sizeseq where size_grp <> '1218' and isnumeric(size) = 0
	group by size_grp


	select distinct style_no, b.size_grp 
	into #size_is_str
	from #style_size_is_str a join #size_str b on 
	 a.min_size between  b.min_size and b.max_size and  a.max_size between b.min_size and b.max_size 

	select style_no, min(size) min_size, max(size) max_size, '' size_group 
	into #style_size_is_numeric
	from pos_sku  a
	where exists(select *  from #a aa where a.style_no = aa.style_no ) and 
		isnumeric(a.size) = 1
	group by style_no

	select size_grp, min(size) min_size, max(size) max_size 
	into #size_numeric
	from ms_sizeseq where size_grp <> '1218' and isnumeric(size) = 1
	group by size_grp


	select distinct a.style_no, b.size_grp 
	into #size_is_numeric
	from #style_size_is_numeric a join #size_numeric b on 
	convert(dec, a.min_size) between convert(dec, b.min_size) and b.max_size and convert(dec, a.max_size) between b.min_size and b.max_size 


declare @upd_id int
select @upd_id = convert(int, var_data) from igs_parm with(updlock) where var_name = 'gv_upd_id'

update ms_style set size_group = a.size_grp, upd_id = @upd_id
from #size_is_str a
where ms_style.style_no = a.style_no
select @@rowcount aa
update ms_style set size_group = a.size_grp, upd_id = @upd_id
from #size_is_numeric a
where ms_style.style_no = a.style_no
select @@rowcount bb
select distinct a.style_no from #size_is_str a join pos_sku b on a.style_no = b.style_no
where not exists(select * from ms_sizeseq aa where a.size_grp = aa.size_grp and b.size = aa.size)
 
 select *   from igsnet.dbo.ms_style a join pos_sku b on a.style_no = b.style_no where size_group = '1218' 
commit tran